package com.futuremind.recyclerviewfastscroll.viewprovider;

import ohos.agp.components.AttrHelper;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;

import com.futuremind.recyclerviewfastscroll.FastScroller;
import com.futuremind.recyclerviewfastscroll.ResourceTable;
import com.futuremind.recyclerviewfastscroll.Utils;

import java.util.Optional;

/**
 * Created by Michal on 05/08/16.
 *
 * @since 2021-04-10
 */
public class DefaultScrolledViewProvider extends ScrollerViewProvider {
    protected Component bubble;
    protected Component handle;
    private static final int defaultValue = 6;
    private static final int horizontalInsetDefault = 8;
    private static final int handleHeightDefault = 30;
    private static final int paddingDefault = 20;
    private static final float mOffDefault = 2f;

    /**
     * 定义滚动条
     *
     * @param container The container {@link FastScroller} for the view to inflate properly.
     * @return 滚动条
     */
    @Override
    public Component provideHandleView(ComponentContainer container) {
        handle = new Image(getContext());
        int verticalInset = getScroller().isVertical() ? 0 : AttrHelper.vp2px(horizontalInsetDefault, getContext());
        int horizontalInset = !getScroller().isVertical() ? 0 : AttrHelper.vp2px(horizontalInsetDefault, getContext());
        ShapeElement shapeElement = new ShapeElement(getContext(), ResourceTable.Graphic_fastscroll__default_handle);
        Utils.setBackground(handle, shapeElement);
        int handleWidth =
            AttrHelper.vp2px(getScroller().isVertical() ? defaultValue : handleHeightDefault, getContext());
        int handleHeight =
            AttrHelper.vp2px(getScroller().isVertical() ? handleHeightDefault : defaultValue, getContext());
        ComponentContainer.LayoutConfig layoutConfig = new ComponentContainer.LayoutConfig(handleWidth, handleHeight);
        layoutConfig.setMargins(horizontalInset, verticalInset, horizontalInset, verticalInset);
        handle.setLayoutConfig(layoutConfig);
        return handle;
    }

    /**
     * 定义气泡框
     *
     * @param container The container {@link FastScroller} for the view to inflate properly.
     * @return 气泡框
     */
    @Override
    public Component provideBubbleView(ComponentContainer container) {
        int radius = AttrHelper.vp2px(paddingDefault, getContext());
        float[] radii = new float[]{
            radius, radius,
            radius, radius,
            0, 0,
            radius, radius
        };
        ShapeElement shapeElement2 = new ShapeElement(getContext(), ResourceTable.Graphic_fastscroll__default_bubble);
        shapeElement2.setCornerRadiiArray(radii);
        bubble = LayoutScatter.getInstance(getContext())
            .parse(ResourceTable.Layout_fastscroll_defalut_bubble, container, false);
        bubble.setBackground(shapeElement2);
        return bubble;
    }

    @Override
    public Text provideBubbleTextView() {
        return (Text) bubble;
    }

    @Override
    public float getBubbleOffset() {
        return getScroller().isVertical() ? ((float) handle.getHeight()) / mOffDefault - bubble.getHeight()
            : ((float) handle.getWidth()) / mOffDefault - bubble.getWidth();
    }

    @Override
    protected ViewBehavior provideHandleBehavior() {
        return null;
    }

    @Override
    protected ViewBehavior provideBubbleBehavior() {
        return new DefaultBubbleBehavior(new VisibilityAnimationManager.Builder(bubble)
            .withPivotX(1f).withPivotY(1f).build());
    }
}
